
//
//  CYMyCarLoanVC.m
//  CreditAddressBookEE
//
//  Created by LE on 16/2/19.
//  Copyright © 2016年 Lee. All rights reserved.
//

#import "CYMyCarLoanVC.h"
#import "CYRewardSearchCell.h"
#import "CYDiscoveryHttpTool.h"
#import "CYCompanyUserTool.h"
#import "MBProgressHUD+LE.h"
#import "UITableViewCell+Category.h"
#import "CYTool.h"
#import "CYSimpleLoanM.h"
#import "CYCreditTool.h"
#import "LEPresentingAnimatorTool.h"
#import "CYApplyToViewVC.h"
#import "CYMyLoanAppliedInfoVC.h"
#import "UIInfomationView.h"
#import "CYLoginHttpTool.h"

@interface CYMyCarLoanVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, weak) UITableView *tableview;
@property(nonatomic, strong) NSArray *data;

@end

@implementation CYMyCarLoanVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTable];
    [self loadData];
}

- (void)setupTable {
    UITableView *table = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:table];
    self.tableview = table;
    table.delegate = self;
    table.dataSource = self;
    table.backgroundColor = [UIColor groupTableViewBackgroundColor];
    table.separatorStyle = UITableViewCellSeparatorStyleNone;
    [CYTool setExtraCellLineHiddenWithTableView:table];
}

- (void)loadData {
    CYUser *user = [CYLoginHttpTool userData].user;
    if (user) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYDiscoveryHttpTool getLoanListByCompanyWithParam:@{@"userId": user.userId, @"token": user.token, @"page": @"0"} success:^(NSArray *simpleList) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            self.data = simpleList;
            [self.tableview reloadData];
        } failure:^(NSString *message, BOOL isLogin) {
            [MBProgressHUD hideHUDForView:self.view animated:NO];
            [MBProgressHUD showError:message];
            if (!isLogin) {
                [self.navigationController popToRootViewControllerAnimated:YES];
                [CYCompanyUserTool logoutWithController:self];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CYSimpleLoanM *model = self.data[indexPath.row];
    CYRewardSearchCell *cell = [CYRewardSearchCell cellWithTableView:tableView];
    cell.licencePlateLabel.text = model.carNumber;
    cell.moneyLabel.text = model.rewardMoney;
    cell.companyName.text = model.companyName;
    cell.companyPhone.text = model.companyPhone;
    cell.timeLabel.text = [CYCreditTool timewithLongLongValue:model.limitTime.longLongValue formatter:@"yyyy-MM-dd"];
    cell.name.text = model.name;
    cell.phone.text = model.phone;
    cell.idCard.text = model.certificateNumber;
    cell.applyBtn.tag = indexPath.row;
    cell.tipLabel.hidden = NO;
    cell.tipLabel.text = model.statusStr;
    if (model.status.intValue == 3) {
        cell.applyBtn.hidden = NO;
        cell.tipConstraint.constant = 4;
        [cell.applyBtn setTitle:@"查看" forState:UIControlStateNormal];
        [cell.applyBtn addTarget:self action:@selector(applyBtn:) forControlEvents:UIControlEventTouchUpInside];
    } else {
        cell.tipConstraint.constant = -40;
        cell.applyBtn.hidden = YES;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 180;
}
/*!
 *  @brief  查看已申请的悬赏情况
 */
- (void)applyBtn:(UIButton *)btn {
    CYSimpleLoanM *model = self.data[btn.tag];
    CYWeakObject(wself, self);
    [self getDisplayWithModel:model completion:^(BOOL isDisplay, BOOL isPay) {
        if (isDisplay) {
            CYMyLoanAppliedInfoVC *loanInfo = [[CYMyLoanAppliedInfoVC alloc] init];
            loanInfo.model = model;
            [wself.navigationController pushViewController:loanInfo animated:NO];
        } else {
            CYApplyToViewVC *to = [[CYApplyToViewVC alloc] init];
            to.tip = [NSString stringWithFormat:@"         查看车辆异常将冻结账户余额%@元，用于支付悬赏金，是否确定查看？", model.rewardMoney];
            [LEPresentingAnimatorTool presentingAnimatorWithController:to showIn:wself completion:nil];
            [to confirmBtnClicked:^{
                if (isPay) {
                    //弹出输入交易密码
                    [wself checkPasswordWithModel:model];
                } else {
                    //提示余额不足
                }
            }];
        }
    }];
}
/*!
 *  @brief
 */
/*!
 *  @brief  获取状态
 *  @param completion isDisplay：是否已查看过；isPay：余额是否充足
 */
- (void)getDisplayWithModel:(CYSimpleLoanM *)model completion:(void(^)(BOOL isDisplay, BOOL isPay))completion {
    [CYDiscoveryHttpTool getDisplayWithParam:@{@"loanRecordId": model.loanRecordId} success:^(BOOL isDisplay, BOOL isPay) {
        if (completion) {
            completion(isDisplay, isPay);
        }
    } failure:^(NSString *message, BOOL isLogin) {
        
    }];
}
/*!
 *  @brief  校验交易密码
 */
- (void)checkPasswordWithModel:(CYSimpleLoanM *)model {
    CYWeakObject(wself, self);
    [UIInfomationView showAlertViewWithTitle:@"请输入交易密码" message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] style:UIAlertViewStylePlainTextInput keyboardType:UIKeyboardTypeNumberPad clickAtIndex:^(UIAlertView *alertView, NSInteger buttonIndex) {
        UITextField *tf = [alertView textFieldAtIndex:0];
        [tf resignFirstResponder];
        if (buttonIndex == 1) {
            CGFloat amount = tf.text.intValue;
            if (amount > 0) {
//                CYCompanyUserModel *user = [CYCompanyUserTool companyUser];
            } else {
                [MBProgressHUD showError:@"输入金额有误"];
            }
            //校验密码
            //密码校验正确,跳转
            CYMyLoanAppliedInfoVC *loanInfo = [[CYMyLoanAppliedInfoVC alloc] init];
            loanInfo.model = model;
            [wself.navigationController pushViewController:loanInfo animated:NO];
        }
    }];
}
@end
